package edu.jptest.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

import edu.jptest.bean.QuestionOrigin;
import edu.jptest.service.IQuestionOriginService;
import edu.jptest.serviceImp.QuestionOriginService;
import edu.jptest.util.UnitLesson;

public class QuestionOriginAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4813219367376684543L;
	private QuestionOrigin questionOrigin;
	private List<QuestionOrigin> questionOriginList;
	List<UnitLesson> unitLessionsList;
	private int unitCount;
	private int lessonCount;
	private List<Integer> unitList;
	private List<Integer> lessonList;
	private Map<Integer, List<Integer>> questionOriginMap;
	private List<String> bookNamesList;

	public List<String> getBookNamesList() {
		return bookNamesList;
	}

	public void setBookNamesList(List<String> bookNamesList) {
		this.bookNamesList = bookNamesList;
	}

	public Map<Integer, List<Integer>> getQuestionOriginMap() {
		return questionOriginMap;
	}

	public void setQuestionOriginMap(
			Map<Integer, List<Integer>> questionOriginMap) {
		this.questionOriginMap = questionOriginMap;
	}

	public List<Integer> getUnitList() {
		return unitList;
	}

	public void setUnitList(List<Integer> unitList) {
		this.unitList = unitList;
	}

	// 根据select选项得到课别列表
	public List<Integer> getLessonList() {
		ValueStack stack = ServletActionContext
				.getValueStack(ServletActionContext.getRequest());
		Object units = stack.findValue("top");
		if (units != null) {
			lessonList = (List<Integer>) questionOriginMap.get(units);
			return lessonList;
		} else {
			return Collections.EMPTY_LIST;
		}
	}

	public void setLessonList(List<Integer> lessonList) {
		this.lessonList = lessonList;
	}

	public int getUnitCount() {
		return unitCount;
	}

	public void setUnitCount(int unitCount) {
		this.unitCount = unitCount;
	}

	public int getLessonCount() {
		return lessonCount;
	}

	public void setLessonCount(int lessonCount) {
		this.lessonCount = lessonCount;
	}

	public QuestionOrigin getQuestionOrigin() {
		return questionOrigin;
	}

	public void setQuestionOrigin(QuestionOrigin questionOrigin) {
		this.questionOrigin = questionOrigin;
	}

	public List<QuestionOrigin> getQuestionOriginList() {
		return questionOriginList;
	}

	public void setQuestionOriginList(List<QuestionOrigin> questionOriginList) {
		this.questionOriginList = questionOriginList;
	}

	public List<UnitLesson> getUnitLessionsList() {
		return unitLessionsList;
	}

	public void setUnitLessionsList(List<UnitLesson> unitLessionsList) {
		this.unitLessionsList = unitLessionsList;
	}

	// 添加教材、单元、课别
	public String addQuestionOrigin() {
		IQuestionOriginService iOriginService = new QuestionOriginService();
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		String[] units = request.getParameterValues("unit");
		for (int i = 0; i < units.length; i++) {
			if (units[i].equals("true")) {
				String start = "start" + (i + 1);
				String end = "end" + (i + 1);
				int startNum = Integer.valueOf(request.getParameter(start));
				int endNum = Integer.valueOf(request.getParameter(end));
				for (int j = startNum; j <= endNum; j++) {
					questionOrigin.setUnit(i + 1);
					questionOrigin.setLesson(j);
					iOriginService.insertQuestionOrigin(questionOrigin);
				}
				// System.out.println("unit "+i+" is true");
			}
		}
		return SUCCESS;
	}

	// 得到教材名、单元数、课数
	public String getBookInfo() {
		IQuestionOriginService iOriginService = new QuestionOriginService();
		List<String> booksNameList = new ArrayList<String>();
		booksNameList = iOriginService.getAllBooksName();
		Iterator<String> iterator = booksNameList.iterator();
		questionOrigin = new QuestionOrigin();
		questionOriginList = new ArrayList<QuestionOrigin>();
		while (iterator.hasNext()) {
			String booksName = (String) iterator.next();
			questionOrigin.setTextbookName(booksName);
			questionOrigin.setUnit(iOriginService.getUnitNum(questionOrigin
					.getTextbookName()));
			questionOrigin.setLesson(iOriginService.getLessonNum(questionOrigin
					.getTextbookName()));
			questionOriginList.add(questionOrigin);
		}
		return SUCCESS;
	}

	// 根据教材名查询单元课别对应关系
	public String getUnitLesson() {
		IQuestionOriginService iOriginService = new QuestionOriginService();
		// unitLessionsList = new ArrayList<UnitLesson>();
		unitLessionsList = iOriginService.getUnitLesson(questionOrigin
				.getTextbookName());
		return SUCCESS;
	}

	// 根据教材名删除教材
	public String deleteQuestionOrigin() {
		IQuestionOriginService iOriginService = new QuestionOriginService();
		if (iOriginService.deleteQuestionOriginByBookName(questionOrigin
				.getTextbookName()))
			return SUCCESS;
		else {
			return INPUT;
		}
	}

	// 添加试题得到教材名列表
	public String getOriginNames() {
		IQuestionOriginService iOriginService = new QuestionOriginService();
		bookNamesList = iOriginService.getAllBooksName();
		return SUCCESS;
	}

	// 添加试题选择教材、题型，得到教材单元、课别列表
	@SuppressWarnings("unchecked")
	public String getOriginSelect() {
		String bookName = "";
		Map sessionMap = ActionContext.getContext().getSession();
		if (null == questionOrigin) {
			bookName = (String) sessionMap.get("bookName");
			if (bookName == null) {
				System.out
						.println("QuestionOriginAction.getOriginSelect()错误：bookName in session is null!!!!");
			}
			questionOrigin = new QuestionOrigin();
			questionOrigin.setTextbookName(bookName);
		} else {
			bookName = questionOrigin.getTextbookName();
			sessionMap.put("bookName", questionOrigin.getTextbookName());
		}

		IQuestionOriginService iOriginService = new QuestionOriginService();
		questionOriginList = iOriginService.getUnitLessonList(bookName);
		Iterator<QuestionOrigin> it = questionOriginList.iterator();

		unitList = new ArrayList<Integer>();
		questionOriginMap = new HashMap<Integer, List<Integer>>();

		while (it.hasNext()) {
			QuestionOrigin origin = (QuestionOrigin) it.next();
			if (!unitList.contains(origin.getUnit())) {
				unitList.add(origin.getUnit());
				List<Integer> lessons = new ArrayList<Integer>();
				lessons.add(origin.getLesson());
				questionOriginMap.put(origin.getUnit(), lessons);
			} else {
				List<Integer> lessons = questionOriginMap.get(origin.getUnit());
				if (lessons == null) {
					List<Integer> lessons2 = new ArrayList<Integer>();
					lessons2.add(origin.getLesson());
					questionOriginMap.put(origin.getUnit(), lessons2);
				} else {
					lessons.add(origin.getLesson());
					questionOriginMap.put(origin.getUnit(), lessons);
				}
			}
		}
		return SUCCESS;
	}

	// 查询试题选择教材名，得到教材单元、课别列表
	public String getOriginUnitLesson() {
		IQuestionOriginService iOriginService = new QuestionOriginService();
		questionOriginList = iOriginService.getUnitLessonList(questionOrigin
				.getTextbookName());
		Iterator<QuestionOrigin> it = questionOriginList.iterator();

		unitList = new ArrayList<Integer>();
		questionOriginMap = new HashMap<Integer, List<Integer>>();

		while (it.hasNext()) {
			QuestionOrigin origin = (QuestionOrigin) it.next();
			if (!unitList.contains(origin.getUnit())) {
				unitList.add(origin.getUnit());
				List<Integer> lessons = new ArrayList<Integer>();
				lessons.add(origin.getLesson());
				questionOriginMap.put(origin.getUnit(), lessons);
			} else {
				List<Integer> lessons = questionOriginMap.get(origin.getUnit());
				if (lessons == null) {
					List<Integer> lessons2 = new ArrayList<Integer>();
					lessons2.add(origin.getLesson());
					questionOriginMap.put(origin.getUnit(), lessons2);
				} else {
					lessons.add(origin.getLesson());
					questionOriginMap.put(origin.getUnit(), lessons);
				}
			}
		}
		return SUCCESS;
	}

}
